//
//  MTSetWalletPasswordController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/17.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTSetWalletPasswordController.h"
#import "MTInputPasswordView.h"
#import "MTWalletViewController.h"
#import "MTWalletApiManager.h"

@interface MTSetWalletPasswordController ()
@property(nonatomic,strong) MTInputPasswordView *inputpasswordView;

@property(nonatomic,copy) NSString *fristPass;

@end

@implementation MTSetWalletPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置密码";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _inputpasswordView = [MTInputPasswordView inputPasswordView];
    [self.view addSubview:_inputpasswordView];
    [_inputpasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(150);
        make.height.mas_equalTo(@(44));
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.left.mas_equalTo(self.view.mas_left).offset(20);
    }];
    
    @weakify(self);
    _inputpasswordView.completeBlock = ^(NSString *password) {
        @strongify(self);
        if(self.fristPass == nil)
        {
            self.fristPass = password;
            self.title = @"请重复输入密码";
            [self.inputpasswordView clearPassword];
        }else
        {
            if([self.fristPass isEqualToString:password]) //二次密码输入一样设置密码
            {
            
                [[MTWalletApiManager manager] setWalletWithPassword:password phone:self.phone code:self.verCode countryCode:self.countryCode complation:^(MTError *error) {
                     [MBProgressHUD hideHUD];
                    if(error == nil)
                    {
                        [MBProgressHUD showSuccess:@"设置成功"];
                        [self.navigationController popToViewController:[MTWalletApiManager manager].fromVc animated:YES];
                    }else
                    {
                         [MBProgressHUD showSuccess:error.errorDescription];
                    }
                }];
            }else
            {
                self.fristPass = nil;
                [self.inputpasswordView clearPassword];
                self.title = @"设置密码";
                UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:@"两次输入不一致，请重新设置" message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           
                }];
                [alterVc addAction:action1];
                [self presentViewController:alterVc animated:YES completion:nil];
            }
        }
    };
    [_inputpasswordView becomeFirstResponder];
}

-(void)goBack
{
    [super goBack];
    [self.inputpasswordView clearPassword];
}



@end
